package com.campuscarbooking.api.listener;

import com.campuscarbooking.api.config.RabbitMQConfig;
import com.campuscarbooking.api.dto.message.ReservationNotificationMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * Listener for reservation notification messages from RabbitMQ
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class ReservationNotificationListener {

    @RabbitListener(queues = RabbitMQConfig.RESERVATION_NOTIFICATION_QUEUE)
    public void handleReservationNotification(ReservationNotificationMessage message) {
        log.info("Received reservation notification message: {}", message);

        try {
            // Handle different notification types
            switch (message.getNotificationType()) {
                case "CREATED":
                    handleReservationCreated(message);
                    break;
                case "CONFIRMED":
                    handleReservationConfirmed(message);
                    break;
                case "CANCELLED":
                    handleReservationCancelled(message);
                    break;
                case "REMINDER":
                    handleReservationReminder(message);
                    break;
                default:
                    log.warn("Unknown notification type: {}", message.getNotificationType());
            }
        } catch (Exception e) {
            log.error("Error processing reservation notification message", e);
        }
    }

    private void handleReservationCreated(ReservationNotificationMessage message) {
        log.info("Processing reservation created notification for reservation ID: {}", message.getReservationId());
        // In a real implementation, this could:
        // 1. Send an SMS notification
        // 2. Send an email notification
        // 3. Update analytics
        // 4. Trigger other business processes
    }

    private void handleReservationConfirmed(ReservationNotificationMessage message) {
        log.info("Processing reservation confirmed notification for reservation ID: {}", message.getReservationId());
        // Similar to above, handle confirmation-specific logic
    }

    private void handleReservationCancelled(ReservationNotificationMessage message) {
        log.info("Processing reservation cancelled notification for reservation ID: {}", message.getReservationId());
        // Handle cancellation-specific logic
    }

    private void handleReservationReminder(ReservationNotificationMessage message) {
        log.info("Processing reservation reminder notification for reservation ID: {}", message.getReservationId());
        // Handle reminder-specific logic
    }
} 